// Copyright 2010, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v200909;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v200909.cm.ApiError;
import com.google.api.adwords.v200909.cm.ApiException;
import com.google.api.adwords.v200909.cm.BiddingTransitionError;
import com.google.api.adwords.v200909.cm.Campaign;
import com.google.api.adwords.v200909.cm.CampaignOperation;
import com.google.api.adwords.v200909.cm.CampaignReturnValue;
import com.google.api.adwords.v200909.cm.CampaignServiceInterface;
import com.google.api.adwords.v200909.cm.ConversionOptimizer;
import com.google.api.adwords.v200909.cm.ConversionOptimizerAdGroupBids;
import com.google.api.adwords.v200909.cm.ConversionOptimizerBiddingTransition;
import com.google.api.adwords.v200909.cm.Operator;
import com.google.api.adwords.v200909.cm.PricingModel;

/**
 * This example shows how to check for conversion optimizer eligibility by
 * attempting to set the bidding transition with the validate only header set to
 * true.
 *
 * Tags: CampaignService.get
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class GetConversionOptimizerEligibility {
  public static void main(String[] args) {
    try {
      // Log SOAP XML request and response.
      AdWordsServiceLogger.log();

      // Get AdWordsUser from "~/adwords.properties".
      AdWordsUser user = new AdWordsUser();

      // Get the validation CampaignService.
      CampaignServiceInterface campaignService =
          user.getValidationService(AdWordsService.V200909.CAMPAIGN_SERVICE);

      long campaignId = Long.parseLong("12");

      // Create campaign.
      Campaign campaign = new Campaign();
      campaign.setId(campaignId);

      // Create bidding transition.
      ConversionOptimizerBiddingTransition conversionOptimizerBiddingTransition
          = new ConversionOptimizerBiddingTransition();

      // Create conversion optimizer bidding strategy.
      ConversionOptimizer conversionOptimizer = new ConversionOptimizer();
      conversionOptimizer.setPricingModel(PricingModel.CONVERSIONS);
      conversionOptimizerBiddingTransition.setTargetBiddingStrategy(conversionOptimizer);

      // Create converstion optimizer ad group bids.
      ConversionOptimizerAdGroupBids conversionOptimizerAdGroupBids
          = new ConversionOptimizerAdGroupBids();
      conversionOptimizerBiddingTransition.setExplicitAdGroupBids(conversionOptimizerAdGroupBids);

      // Create operations.
      CampaignOperation operation = new CampaignOperation();
      operation.setBiddingTransition(conversionOptimizerBiddingTransition);
      operation.setOperand(campaign);
      operation.setOperator(Operator.SET);

      CampaignOperation[] operations = new CampaignOperation[] {operation};

      try {
        // Check that the campaign is eligible for conversion optimization.
        CampaignReturnValue result = campaignService.mutate(operations);

        System.out.println("Campaign with id \"" + campaign.getId()
            + "\" is eligible to use conversion optimizer.");
      } catch (ApiException e) {
        for (ApiError error : e.getErrors()) {
          if (error instanceof BiddingTransitionError) {
            BiddingTransitionError biddingTransitionError = (BiddingTransitionError) error;
            System.out.println("Campaign with id \"" + campaign.getId()
                + "\" is not eligible to use conversion optimizer for reason \""
                + biddingTransitionError.getReason() + "\".");
          } else {
            // Throw non-bidding transition related exception.
            throw e;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
